/**=========================================================
 * Service: monthpicker-combine.js
 =========================================================*/

 tzadmin.service('CombineMonthpicker', ['Utils',
    function(utils) {
        'use strict';
        
        return {
            init: function($scope,options) {
                var date = new Date();
                date.setDate(1);
                
                var defaultOptions = {
                    optionModel: 'monthRange',
                    startMonth: utils.addDate(date, "month", -6),
                    endMonth: date
                }

                var finalOptions = angular.extend(defaultOptions,options);
                
                if (utils.isEmpty($scope.combineMonthPickers)){
                    $scope.combineMonthPickers = {}
                }
                
                $scope.combineMonthPickers[finalOptions.optionModel] = {};
                //开始日期
                $scope.combineMonthPickers[finalOptions.optionModel].startMonth = {
                    date: finalOptions.startMonth,
                    dateOptions: {
                        datepickerMode: 'month',
                        minMode: 'month',
                        startingDay: 1,
                        maxDate: finalOptions.endMonth
                    },
                    opened: false
                };
                //结束日期
                $scope.combineMonthPickers[finalOptions.optionModel].endMonth = {
                    date: finalOptions.endMonth,
                    dateOptions: {
                        datepickerMode: 'month',
                        minMode: 'month',
                        startingDay: 1,
                        minDate: finalOptions.startMonth
                    },
                    opened: false
                }
                
                return {
                    scope: $scope,
                    options: finalOptions,
                    addParam: function(params,format){
                        if (utils.isEmpty(format)){
                            format = 'yyyyMM';
                        }
                        params.startMonth = utils.dateFormat(new Date($scope.combineMonthPickers[this.options.optionModel].startMonth.date),format);
                        params.endMonth = utils.dateFormat(new Date($scope.combineMonthPickers[this.options.optionModel].endMonth.date),format);
                    },
                    getStartMonth: function(){
                        return new Date($scope.combineMonthPickers[this.options.optionModel].startMonth.date);
                    },
                    getEndMonth: function(){
                        return new Date($scope.combineMonthPickers[this.options.optionModel].endMonth.date);
                    }
                }
            }
        }
    }
]);
